package com.ygxsk.carhome.ui.adapter;

import static com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions.withCrossFade;

import android.app.Activity;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.ygxsk.carhome.R;

import java.util.ArrayList;

/**
 * Autuhor:itchen
 * Time:2020/3/25 0025  17:10
 * Description: this is 九宫格内容 在商品的详情里的内容
 */
public class ImageInDetailAdapter extends BaseAdapter {

    private final Activity mActivity;
    private ArrayList<String> filesReplace = new ArrayList<>();

    public ImageInDetailAdapter(Activity _activity, ArrayList<String> _filesReplace) {
        this.mActivity = _activity;
        this.filesReplace = _filesReplace;
    }


    @Override
    public int getCount() {
        return filesReplace == null ? 0 : filesReplace.size();
    }

    @Override
    public Object getItem(int position) {

        if (position > getCount()) {
            return null;
        }

        return filesReplace.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    public void clearData() {
        if (filesReplace != null) {
            if (!filesReplace.isEmpty()) {
                filesReplace.clear();
            }
            notifyDataSetChanged();
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (mActivity != null) {

            ImageView pview = new ImageView(mActivity);

            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int) (mActivity.getResources().getDisplayMetrics().density * 94),//
                    (int) (mActivity.getResources().getDisplayMetrics().density * 94));

            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);

            pview.setLayoutParams(layoutParams);//
            //设置图片的位置是居中

            pview.setScaleType(ImageView.ScaleType.CENTER_CROP);//

            //填充图片内容itchen做改造
            String imageUrl = filesReplace.get(position);

            Glide.with(mActivity)//
                    .asBitmap()//
                    .load(imageUrl)//
                    .transition(withCrossFade())//
                    .fallback(R.drawable.iconerrorone)//
                    .error(R.drawable.iconerrorone)//
                    .placeholder(R.drawable.iconerrorone)//
                    .into(new BitmapImageViewTarget(pview) {//这个是正常的九宫格图片内容。
                        @Override
                        protected void setResource(Bitmap resource) {//
                            super.setResource(resource);//
                            //圆角图片代码。
                        }
                    });


            // 把PhotoView当普通的控件把触摸功能关掉
            //pview.disenable();

            return pview;

        }
        return null;
    }
}
